﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Subgurim.Controles;
using System.Net;
using System.Text;
using System.IO;
using System.Globalization;
using System.Drawing;
using System.Data.SqlClient;

public partial class UserControl_Gmap : System.Web.UI.UserControl
{
    private string GID;
    public string _GID
    {
        get
        {
            return GID;
        }
        set
        {
            GID = value;
        }
    }
    private string Address1;
    public string _Address1
    {
        get
        {
            return Address1;
        }
        set
        {
            Address1 = value;
        }
    }
    private string Address2;
    public string _Address2
    {
        get
        {
            return Address2;
        }
        set
        {
            Address2 = value;
        }
    }
    private string City;
    public string _City
    {
        get
        {
            return City;
        }
        set
        {
            City = value;
        }
    }
    private string State;
    public string _State
    {
        get
        {
            return State;
        }
        set
        {
            State = value;
        }
    }

    private string strTest = string.Empty;
    public string StrTest
    {
        get { return strTest; }
        set { strTest = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
            DisplayMap();

    }




    protected string TrimDescription(string Address, string City, string State)
    {
        string add = Address + " " + City + " " + State + " " ;
        add = add.Replace(" ", "+");
        add += "&mrt=yp";
        return add;
    }
    
    public void DisplayMap()
    {
        try
        {
            Subgurim.Controles.GMap GMap1 = new Subgurim.Controles.GMap();
            GMap1.ID = GID;
            GMap1.Width = 200;
            GMap1.Height = 200;
            
            string ADD = TrimDescription(Address1, City, State);
            string mapItPath = "https://maps.google.com/maps?q=" + ADD;
                                                                                     
            //  GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.click, "function() {alert('ranjana');}"));
            GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.click, "function() { window.open('" + mapItPath + "', '_blank');}"));
           

            string Address = Address1 + " " + City + " " + State;
            string sMapKey;
            sMapKey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"].ToString();
            double Latitude, Longitude;
            string popupMsg;

            const string path = "https://maps.google.com/maps/geo?output=csv&oe=utf-8&q={0}&key={1}";
            using (WebResponse response = WebRequest.Create(string.Format(CultureInfo.InvariantCulture, path, Address, sMapKey)).GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                string[] parts = reader.ReadToEnd().Split(',');
                Latitude = double.Parse(parts[2], CultureInfo.InvariantCulture);
                Longitude = double.Parse(parts[3], CultureInfo.InvariantCulture);

            }

            GeoCode GeoCode = new GeoCode();
            GLatLng gLatLng = new GLatLng(Latitude, Longitude);
            GMap1.setCenter(gLatLng, 10, GMapType.GTypes.Normal);
            GMarker oMarker = new GMarker(gLatLng);
            GMap1.addGMarker(oMarker);
            GMap1.enableDragging = false;
            popupMsg = Address;
            //GInfoWindow window = new GInfoWindow(oMarker, popupMsg, true);
            // GMap1.enableDragging = true;
            // GMap1.addInfoWindow(window);
            PlaceHolder1.Controls.Add(GMap1);
        }
        catch { lblMsg.Text = "Invalid Address !"; }
    }
}
